package com.zhiwang.xiaoqu.ui.withdraw

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.zhiwang.xiaoqu.R
import com.zhiwang.xiaoqu.di.annotation.Injectable
import com.zhiwang.xiaoqu.di.component.DaggerUserComponent
import com.zhiwang.xiaoqu.di.module.AutoDisposeModule
import com.zhiwang.xiaoqu.ktextension.setImmersive
import com.zhiwang.xiaoqu.mvp.model.response.WithDrawRecord
import com.zhiwang.xiaoqu.mvp.presenter.UserPresenter
import com.zhiwang.xiaoqu.ui.BaseListActivity
import dp
import javax.inject.Inject

class WithdrawListActivity : BaseListActivity<WithDrawRecord, WithdrawListActivity.WithdrawViewHolder>(),Injectable {

    @Inject
    lateinit var userPresenter: UserPresenter

    var page = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        window.setImmersive(Color.TRANSPARENT, true)
        super.onCreate(savedInstanceState)
        setUpMargin()
        userPresenter.getWithDrawRecordList(page, ::onRefreshData, ::onError)
    }


    private fun setUpMargin() {
        val recyclerView = recyclerView()
        recyclerView.setBackgroundResource(R.drawable.round_bg5)
        val layoutParams = recyclerView.layoutParams as SmartRefreshLayout.LayoutParams
        layoutParams.leftMargin = 15.dp
        layoutParams.topMargin = 10.dp
        layoutParams.bottomMargin = 10.dp
        layoutParams.rightMargin = 15.dp
        recyclerView.layoutParams = layoutParams
    }


    override fun startLoadMore() {
        userPresenter.getWithDrawRecordList(page + 1, ::onLoadMoreData, ::onError)
    }

    override fun startRefresh() {
        window.decorView.postDelayed({
            stopRefresh()
        }, 150)
    }

    override fun onLoadMoreComplete() {
        page++
    }

    override fun onRefreshComplete() {
        page = 1
    }

    override fun getToolBarTitle() = "提现记录"

    override fun getLayoutRes() = R.layout.item_withdraw_layout

    override fun getHeaderLayoutRes() = R.layout.item_withdraw_header

    override fun whenBindViewHolder(holder: WithdrawViewHolder, item: WithDrawRecord?) {
        item?.apply {
            holder.date?.text = item.date
            holder.sum?.text = item.money
            holder.state?.text = item.statusText
        }
    }

    class WithdrawViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val date: TextView? = itemView.findViewById(R.id.date)
        val sum: TextView? = itemView.findViewById(R.id.sum)
        val state: TextView? = itemView.findViewById(R.id.state)
    }

}
